<template>
  <div class="tree-container flex-col">
    <el-input
      v-if="treeData.isSearchShow !== false"
      v-model="filterText"
      size="small"
      type="text"
      placeholder="请输入过滤关键词"
    />
    <div v-if="treeData.title.isShow" class="tree-title flex-row-sb">
      <div>{{ treeData.title.text }}</div>
      <div class="btn-container">
        <el-button type="text" @click="handleOpen">
          <i class="iconfont icon-tree" />
        </el-button>
      </div>
    </div>
    <el-tree
      ref="tree"
      class="filter-tree"
      :data="treeData.data"
      :props="treeData.defaultProps"
      highlight-current
      :default-expand-all="false"
      :filter-node-method="filterNode"
      :expand-on-click-node="false"
      :node-key="nodeKey"
      :show-checkbox="treeData.showCheckbox === true"
      :style="computeTreeStyle"
      :check-strictly="treeData.checkStrictly"
      @node-click="nodeClick"
      @check="checkChange"
    >
      <div slot-scope="{ node }" class="custom-tree-node flex-row-sb">
        <div>
          <i
            v-if="node.data.icon"
            :style="{ backgroundImage: 'url(' + node.data.icon + ')' }"
            class="img-icon"
          />
          <i
            v-else-if="treeData.showTree && node.level === 1"
            class="iconfont icon-daxingzhiwu"
            style="color: #00dd00; font-size: 13px"
          />
          <i
            v-else-if="treeData.showFile"
            class="iconfont icon-24gl-folderOpen"
            style="color: #3b7cff; margin-right: 8px"
          />
          <span>{{ node.label }}</span>
        </div>
        <slot name="appendContent" :row="node" />
      </div>
    </el-tree>
  </div>
</template>

<script>
export default {
  name: "Tree",
  props: {
    treeData: {
      type: Object,
      required: true
    },
    nodeKey: {
      type: String,
      default: "id"
    },
    // 树的高度
    height: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      dialogVisible: false,
      filterText: "",
      activeTab: "1",
      form: {},
      isSelectAll: false
    };
  },
  computed: {
    computeTreeStyle() {
      const resObj = {};
      if (this.height) {
        resObj.maxHeight = this.height;
      }
      return resObj;
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    handleSelect(index) {
      this.activeTab = index;
    },
    handleOpen() {
      this.dialogVisible = true;
    },
    filterNode(value, data) {
      if (!value) return true;
      return data[this.treeData.defaultProps.label].indexOf(value) !== -1;
    },
    nodeClick(obj, node) {
      this.$emit("treeNodeClick", obj, node);
    },
    checkChange(obj, statusObj) {
      this.$emit("checkChange", statusObj);
    },
    setCheckedKeys(checkedKeys) {
      this.$refs.tree.setCheckedKeys(checkedKeys);
    }
  }
};
</script>

<style lang="scss" scoped>
.tree-container {
  width: 100%;
  box-sizing: border-box;
  background: #fff;
  overflow: hidden;
  flex: 1;
  max-height: 100%;
  .tree-title {
    padding: 0px 10px;
    margin-top: 10px;
    color: #333;
    border-bottom: 1px solid #f0f0f0;
    .iconfont {
      color: #ccc;
      transition: all 0.2s;
      &:hover {
        color: #4fc3f7;
      }
      &:active {
        color: #2962ff;
      }
    }
  }
  .el-tree {
    margin-top: 16px;
    overflow: auto;
    flex: 1;
    max-height: 700px;
    padding-bottom: 20px;
  }
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
    .img-icon {
      width: 16px;
      height: 16px;
      background-size: cover;
      background-repeat: no-repeat;
      display: inline-block;
      margin-right: 4px;
      margin-left: 8px;
    }
  }

  .dialog-content {
    width: 100%;
    height: 300px;
    box-sizing: border-box;
    align-items: flex-start;

    .switch-control {
      height: 100%;
      padding-right: 20px;
    }

    .right-container {
      flex: 1;
      height: 100%;
      margin-left: 20px;
    }

    .main-container {
      width: 100%;
      height: 100%;

      .upload-control {
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-between;
      }
    }
  }
}
</style>

<style lang="scss">
// 修改树结构前面得小三角为 + 和 -
.tree-container {
  .el-tree-node__expand-icon.expanded {
    transform: rotate(0deg);
  }
  .el-icon-caret-right:before {
    content: "\e6d9";
    font-size: 14px;
    border: 1px solid #eff2f7;
  }
  .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
    content: "\e6d8";
    font-size: 14px;
    border: 1px solid #eff2f7;
  }
  .is-leaf::before {
    border: none;
  }
  .el-tree-node__expand-icon.is-leaf {
    margin-right: 2px;
  }
}
</style>
